#include <stdio.h>
#include <stdlib.h>
#include "linktest.h"


Linknode *creat()
{	
	Linknode *H=malloc(sizeof(Linknode));
	H->data=0;
	H->next=NULL;

	return H;
}

int Hinsert(Linknode *H)
{
	H->data++;
	Linknode *p=malloc(sizeof(Linknode));
	printf("plz input yournum\n");
	scanf("%d",&(p->data));
	p->next=H->next;
	H->next=p;

	return 0;
}

int Tinsert(Linknode *H)
{
	H->data++;
	Linknode *p=malloc(sizeof(Linknode));
	printf("plz input num\n");
	scanf("%d",&(p->data));
	while(H->next!=NULL)
	{
		H=H->next;
	}
	p->next=H->next;
	H->next=p;
	
	return 0;
}

int posinsert(Linknode *H,int pos)
{
	H->data++;
	Linknode *p=malloc(sizeof(Linknode));
	int i=0;
	for(i=0;i<pos;i++)
	{
		H=H->next;
	}
	printf("plz input num\n");
	scanf("%d",&(p->data));
	p->next=H->next;
	H->next=p;

	return 0;
}

int Hdelete(Linknode *H)
{
	H->data--;
	Linknode *p=H->next;
	H->next=p->next;

	free(p);

	return 0;
}

int Tdelete(Linknode *H)
{
	H->data--;
	while(H->next->next!=NULL)
	{
		H=H->next;
	}

	Linknode *p=H->next;
	H->next=p->next;

	free(p);

	return 0;
}

int posdelete(Linknode *H,int pos)
{
	H->data--;
	int i=0;
	for(i=0;i<pos-1;i++)
	{
		H=H->next;
	}
	Linknode *p=H->next;
	H->next=p->next;

	free(p);

	return 0;

}

int returnlink(Linknode *H)
{
	Linknode *p=H->next->next;
	H->next->next=NULL;
	Linknode *q=p;

	while(p!=NULL)
	{
		q=p;
		p=p->next;
		q->next=H->next;
		H->next=q;
	}

	return 0;
}

int sortlink(Linknode *H)
{
	Linknode *p=H;
	int len=H->data;
	int temp;
	int i=0;
	int j=0;
	for(i=0;i<len-1;i++)
	{
		for(j=0;j<len-i-1;j++)
		{
			if((p->data)>(p->next->data))
			{
				temp=p->data;
				p->data=p->next->data;
				p->next->data=temp;
			}
			p=p->next;
		}
		p=H;
	}

	return 0;
}

int view(Linknode *H)
{
	while(H->next!=NULL)
	{
		H=H->next;
		printf("%d ",H->data);
	}
	puts(" ");

	return 0;
}
